package main

import (
	"context"
	"fmt"
	"google.golang.org/grpc"
	"mxshop_srvs/order_srv/proto"
)

var orderClient proto.OrderClient
var conn *grpc.ClientConn

func Init() {
	var err error
	conn, err = grpc.Dial("192.168.31.78:50054", grpc.WithInsecure())
	if err != nil {
		panic(err)
	}
	orderClient = proto.NewOrderClient(conn)
}

func main() {
	Init()
	TestCreateCartItem(13, 2, 422)
	//TestCreateCartItem(13, 3, 423)
	//TestCartItemList(13)
	//TestUpdateCartItem(13, 422)
	TestUpdateCartItem(13, 422)
	//TestUpdateCartItem(13, 423)
	//TestDeleteCartItem(13, 422)
	//TestCartItemList(13)
	TestCreateOrder()
	//TestOrderList()
	//TestOrderDetail(1)
	conn.Close()
}

/*
*
添加购物车
*/
func TestCreateCartItem(userId, nums, goodsId int32) {
	rsp, err := orderClient.CreateCartItem(context.Background(), &proto.CartItemRequest{
		UserId:  userId,
		GoodsId: goodsId,
		Nums:    nums,
	})
	if err != nil {
		panic(err)
	}
	fmt.Println(rsp.Id)
}

/*
*
获取购物车的列表
*/
func TestCartItemList(userId int32) {
	list, err := orderClient.CartItemList(context.Background(), &proto.UserInfo{
		Id: userId,
	})
	if err != nil {
		panic(err)
	}
	for _, item := range list.Data {
		fmt.Println(item.Id, item.GoodsId, item.Nums)
	}
}

/*
*
更新购物车
*/
func TestUpdateCartItem(userId, goodsId int32) {
	_, err := orderClient.UpdateCartItem(context.Background(), &proto.CartItemRequest{
		UserId:  userId,
		GoodsId: goodsId,
		Checked: true,
	})
	if err != nil {
		panic(err)
	}
}

/*
*
删除购物车的数据
*/
func TestDeleteCartItem(userId, goodsId int32) {
	_, err := orderClient.DeleteCartItem(context.Background(), &proto.CartItemRequest{
		UserId:  userId,
		GoodsId: goodsId,
	})
	if err != nil {
		panic(err)
	}
}

/*
*
删除购物车
*/
func TestDelCartItem(userId, goodsId int32) {
	_, err := orderClient.DeleteCartItem(context.Background(), &proto.CartItemRequest{
		UserId:  userId,
		GoodsId: goodsId,
	})
	if err != nil {
		panic(err)
	}
}

/*
*
下单
*/
func TestCreateOrder() {
	_, err := orderClient.CreateOrder(context.Background(), &proto.OrderRequest{
		UserId:  13,
		Address: "北京市",
		Name:    "bobby",
		Mobile:  "18739798667",
		Post:    "请尽快发货",
	})
	if err != nil {
		panic(err)
	}
}

/*
*
获取订单列表
*/
func TestOrderList() {
	list, err := orderClient.OrderList(context.Background(), &proto.OrderFilterRequest{
		UserId:      13,
		Pages:       1,
		PagePerNums: 20,
	})
	if err != nil {
		panic(err)
	}
	fmt.Println("订单总数是:", list.Total)
	for _, orderInfo := range list.Data {
		fmt.Println(orderInfo)
	}
}

/*
*
获取订单详情
*/
func TestOrderDetail(orderId int32) {
	orderDetail, err := orderClient.OrderDetail(context.Background(), &proto.OrderRequest{
		Id: orderId,
	})
	if err != nil {
		panic(err)
	}
	fmt.Println(orderDetail.OrderInfo)
	for _, good := range orderDetail.Goods {
		fmt.Println(good)
	}
}
